<!DOCTYPE html>
<html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteAll()"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
								<form id="form_id">
                                <div class="has-feedback">
							                  分类模板名称：<input  type="text"  name="name">
									<input class="btn btn-default" onclick="getData()" value="查询">
                                </div>
								</form>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="tempLateId" class="table table-bordered table-striped table-hover dataTable">
			                      <thead id="dataList">
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th>
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>
									      <th class="sorting">扩展属性</th>
					                      <th class="text-center">操作</th>
			                          </tr>

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input id="name" class="form-control" placeholder="商品类型">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brands" style="width: 100%" multiple>
							<option th:each="brand : ${brandList}" th:value="${brand.id}">
								[[${brand.name}]]
							</option>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="spec" style="width: 100%" multiple>
							<option th:each="spec:${specList}" th:value="${spec.id}">
								[[${spec.specName}]]
							</option>
						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table id="custom" class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td><input class="form-control" placeholder="属性名称"></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="add()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="updateModal1">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <input type="hidden" id="id">
					  <tr>
						  <td>商品类型</td>
						  <td><input name="name" id="name1"  class="form-control" placeholder="商品类型">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="brands1" style="width: 100%" multiple>
								  <option th:each="brand:${brandList}" th:value="${brand.id}">
									  [[${brand.name}]]
								  </option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="spec1" style="width: 100%" multiple>
								  <option th:each="spec:${specList}" th:value="${spec.id}">
									  [[${spec.specName}]]
								  </option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table id="custom1" class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="bodyId">

								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="update()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
    
</body>
<script>
	$(function(){
		getData();
		$("#brands").select2()
		$("#spec").select2()

		$("#brands1").select2()
		$("#spec1").select2()
	})
	function getData(){
		$.ajax({

			url : "/tempLateController/findTempLateAll",
			type : "post",
			dataType : "json",
			data : $("#form_id").serialize(),
			async : false,
			success : function (result){

				if (result.code == 10000){
					var list = result.result;
					var table = "";
					var str = ""
					var str1 = ""
					var str2 = ""
					for (var i = 0; i < list.length; i++) {
						table += '<tr>'
						table += "<td class='' style='padding-right:0px'>";
						table += "<td><input type='checkbox' name='ids' value='"+list[i].id+"'></td>";
						table += "</td>";
						table += '<td>'
						table += list[i].id
						table += '</td>'
						table += '<td>'
						table += list[i].name
						table += '</td>'
						var brandsJson = JSON.parse(list[i].brandIds)
						var specJson = JSON.parse(list[i].specIds)
						var customJson = JSON.parse(list[i].customAttributeItems)
						table += "<td>"
						for (var k = 0 ; k < specJson.length; k++){
							str1 += "," + specJson[k].text
						}
						str1 = str1.substring(1);
						table += str1
						table += "</td>"

						table += "<td>"
						for (var j = 0; j < brandsJson.length; j++) {
							str += "," + brandsJson[j].text
						}
						str = str.substring(1)
						table += str;
						table += '</td>'
						table += "<td>"
						for (var a = 0; a < customJson.length; a++) {
							str2 += "," + customJson[a].text;
						}
						str2 = str2.substring(1);
						table += str2;
						table += "</td>"
						table += "<td>"
						table += "<button type='button' class='btn bg-olive btn-xs' data-toggle='modal' data-target='#updateModal' onclick='toUpdate("+list[i].id+")' '>修改</button>";
						table += "</td>"
						table += '</tr>'

					}
				}else {
					$("#tempLateId").html("<tr><td colSpan='7'>莫得数据</td></tr>")
				}

				$("#dataList").html(table);
			}, error : function (){
				alert("请刷新")
			}
		})
	}

	function add(){
		var select = $("#brands").select2("data");
		var brandList = [];
		for (var i = 0; i < select.length; i++) {
			var brands = {id : select[i].id,text:select[i].text.trim()}
			brandList.push(brands);
		}
		var brandJson = JSON.stringify(brandList)

		var select1 =$("#spec").select2("data");
		var specList =[];
		for (var j = 0; j < select1.length; j++) {
			var specs = {id:select1[j].id,text:select1[j].text.trim()}
			specList.push(specs)
		}
		var specJson = JSON.stringify(specList);

		var trs = $("#custom").find("tr");
		var cusList = [];
		for (var i = 1; i < trs.length; i++) {
			var tds = $(trs[i]).find("td")
			var cus = {text:$(tds[1]).find("input").val()}
			cusList.push(cus)
		}
		var cusListJSON = JSON.stringify(cusList)

		$.ajax({

			url : "/tempLateController/addInfo",
			type : "post",
			dataType : "json",
			data : {name:$("#name").val(),brandIds : brandJson,specIds : specJson,customAttributeItems:cusListJSON},
			async : false,
			success : function (result){
				if (result.code == 10000){
					alert("增加成功")
				}
			},
			error : function (result){
				alert("请刷新")
			}
		})
	}

	function toUpdate(id){

		$.ajax({
			url : "/tempLateController/update/" + id,
			type : "post",
			dataType : "json",
			async : false,
			success : function (result){
				var list = result.result;
				$("#name1").val(list[0].name);
				$("#id").val(list[0].id);
				var brandIds = JSON.parse(list[0].brandIds);
				console.log(brands)
				var ids1 = [];
				$.each(brandIds, function (i, e) {
					ids1.push(e.id)
				})
				$("#brands1").val(ids1).trigger("change");

				var specIds = JSON.parse(list[0].specIds);
				var ids2 = [];
				$.each(specIds, function (i, e) {
					ids2.push(e.id)
				})
				$("#spec1").val(ids2).trigger("change")

				var list = JSON.parse(list[0].customAttributeItems);
				var table = "";
				for (var i = 0; i < list.length; i++) {
					table += '<tr>';
					table += '<td><input type="checkbox" class="icheckbox_square-blue" ></td>';
					table += '<td><input class="form-control" placeholder="属性名称" value="' + list[i].text + '"></td>';
					table += '<td><button type="button" class="btn btn-default" title="删除" onclick="deleteAll()"><i class="fa fa-trash-o"></i> 删除</button></td>';
					table += '</tr>';
				}
				$("#bodyId").html(table);
			},
			error : function (result){
				alert("请刷新")
			}
		})
	}

	function update(){
		var updateId = $("#id").val();
		var updateName = $("#name1").val();
		var brands1 = $("#brands1").select2('data');
		var arr1 = [];
		$.each(brands1,function (i,e){
			var json = {"id":e.id, "text":e.text}
			arr1.push(json)
		})
		var brands = JSON.stringify(arr1);
		var spec1 = $("#spec1").select2('data');
		var arr2 = [];
		$.each(spec1,function (i,e){
			var json1={"id":e.id,"text":e.text};
			arr2.push(json1);
		})
		var specifications = JSON.stringify(arr2);

		var arr3 = [];
		var trs = $("#custom1").find("tr");
		for (var i = 1; i < trs.length; i++){
			var tds = $(trs[i]).find("td")
			var optionName = $(tds[1]).find("input").val();
			arr3.push({text: optionName});
		}
		var CustomAttributeItems = JSON.stringify(arr3);
		$.ajax({
			url:"/tempLateController/update1",
			data:{id : updateId,name:updateName,specIds:specifications,brandIds:brands,customAttributeItems:CustomAttributeItems},
			dataType:"json",
			type:"post",
			success:function (result) {
				if(result.code == 10000){
					alert("修改成功");
					window.location.reload();
				}
			},error:function (result) {
				alert("修改失败")
			}
		})
	}
	function deleteAll(){
		var arr = [];
		var a = $("[name=ids]:checked")
		$(a).each(function(i,e){
			arr.push($(e).val())
		})
		if (confirm("确认删除吗?")){
			$.ajax({
				url:"/tempLateController/deleteAll?arr="+arr,
				dataType: "json",
				type: "post",
				success:function (result){
					if (result.code == 10000){
						alert("删除成功")
						window.location.reload();
					}

				},
				error:function (result){
					alert("错误")
				}
			})
		}
	}

</script>
</html>